//
//  RWMSViewController.m
//  VmiSJ
//
//  Created by 汪的一声就哭了！ on 2017/1/4.
//  Copyright © 2017年 汪的一声就哭了！. All rights reserved.
//

#import "RWMSViewController.h"

@interface RWMSViewController ()
@property (strong, nonatomic) IBOutlet PlaceHoderTextView *textField;
@property (strong, nonatomic) IBOutlet UIButton *wcBtn;
@property (strong, nonatomic) IBOutlet UILabel *numLabel;

@end

@implementation RWMSViewController
- (IBAction)wcAction:(id)sender {
    if ([self.textField.text isEqualToString:@""]) {
        [[SQShowMessage shareMessage] showMessageWithStr:@"请输入内容"];
    }
    else if ([self.titleName isEqualToString:@"任务描述"]) {
        [self.delegate RWMSViewControllerSendRWMSWithStr:self.textField.text];
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = self.titleName;
    self.textField.placeholder = self.placeHolder;
    self.textField.scrollEnabled = NO;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewEditChanged:) name:UITextViewTextDidChangeNotification object:self.textField];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.view addGestureRecognizer:tap];
}
- (void)tapAction:(UITapGestureRecognizer *)tap
{
    [self.view endEditing:YES];
}
#pragma mark ----------- 监听文本改变
-(void)textViewEditChanged:(NSNotification *)obj{
    
    UITextView *textView = (UITextView *)obj.object;
    NSString *toBeString = textView.text;
    NSString *lang = [[UIApplication sharedApplication]textInputMode].primaryLanguage; // 键盘输入模式
    if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入，包括简体拼音，健体五笔，简体手写c
        UITextRange *selectedRange = [textView markedTextRange];
        //获取高亮部分
        UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position) {
            if (toBeString.length > 200) {
                textView.text = [toBeString substringToIndex:200];
            }
        }
        // 有高亮选择的字符串，则暂不对文字进行统计和限制
        else{
        }
    }
    // 中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
    else{
        if (toBeString.length > 200) {
            textView.text = [toBeString substringToIndex:200];
        }
    }
    NSInteger start = [self.textField offsetFromPosition:[textView positionFromPosition:[textView markedTextRange].start offset:0] toPosition:self.textField.beginningOfDocument];
    NSInteger end = [self.textField offsetFromPosition:[textView positionFromPosition:[textView markedTextRange].end offset:0] toPosition:self.textField.beginningOfDocument];
    self.numLabel.text = [NSString stringWithFormat:@"%ld",self.textField.text.length-(start-end)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)dealloc

{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}
@end
